using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Todo.Entity.FormEnrity;

namespace Todo;
[ApiController]
[Route("[controller]/[action]")]
[Authorize]
public class TodoController: ControllerBase
{
    private readonly ITodo _todoService;
    public TodoController(ITodo todoService)
    {
        _todoService = todoService;
    }
    
   
    [HttpGet]
    public ResponseObject<List<Todo>> GetTodoList(Guid id)
    {
            return new ResponseObject<List<Todo>>().OK(_todoService.GetTodoListByUid(id), "查询完成!");
    }

    [HttpPost]
    public ResponseObject<bool> AddTodo([FromBody] TodoForm todoForm)
    {
        var todo = new Todo() { Id = Guid.NewGuid() ,UserId = todoForm.userId, Title = todoForm.title, IsCompleted = false, Description = todoForm.description };
        return new ResponseObject<bool>().OK(_todoService.AddTodo(todo), "添加任务成功!");
    }

    [HttpPut]
    public ResponseObject<bool> UpdateTodo([FromBody] TodoForm todoForm)
    {
        var todo = _todoService.GetTodoListByUid(todoForm.userId).FirstOrDefault(t => t.Id == todoForm.id);
        if (todo!= null)
        {
            todo.Title = todoForm.title;
            todo.Description = todoForm.description;
            return new ResponseObject<bool>().OK(_todoService.UpdateTodo(todo), "任务更新成功!");
        }
        return new ResponseObject<bool>().Error(false,"任务不存在!");
    }
    
    [HttpPut]
    public ResponseObject<bool> UpdateTodoStatus(Guid id)
    {
        return new ResponseObject<bool>().OK(_todoService.UpdateTodoStatus(id), "任务状态更新成功!");
    }
    
    [HttpDelete]
    public ResponseObject<bool> DeleteTodo(Guid id)
    {
        return new ResponseObject<bool>().OK(_todoService.DeleteTodo(id), "任务删除成功!");
    }

 

}